package org.example;

import java.nio.ByteBuffer;

/**
 * <p>  </p>
 *
 * @author Chris
 * @since 2024/4/4 22:59
 */
public class TestByteBuffer {

    public static void main(String[] args) {
        final ByteBuffer buffer = ByteBuffer.allocate(88);
        System.out.printf("init:  pos = %02d, limit = %02d, capacity = %02d\n",
                buffer.position(), buffer.limit(), buffer.capacity());

        final String value = "Netty 权威指南";
        buffer.put(value.getBytes());
        System.out.printf("write: pos = %02d, limit = %02d, capacity = %02d\n",
                buffer.position(), buffer.limit(), buffer.capacity());

        buffer.flip();
        System.out.printf("flip:  pos = %02d, limit = %02d, capacity = %02d\n",
                buffer.position(), buffer.limit(), buffer.capacity());

        final byte[] vArray = new byte[buffer.remaining()];
        buffer.get(vArray);
        final String decodeValue = new String(vArray);
        System.out.println(decodeValue);
    }

}